import Vue from 'vue'
import './plugins/axios'
import App from './App.vue'
import VueRouter from 'vue-router';
import axios from 'axios';
import Index from './views/index.vue'
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
import scroll from 'vue-seamless-scroll'

import StoryList from './views/storylist.vue'
import StoryDetail from './views/storydetail.vue'
import WriteStory from './views/writeStory.vue'
// import Hidden from './views/hidden.vue'
import Center from './views/center.vue'
import MyStory from './views/mystory.vue'
import Comment from './views/comment.vue'
import Likely from './views/likely.vue'
import Collect from './views/collect.vue'
import LoginRegister from './views/loginRegister.vue'
import UpdateUser from './views/updateUser.vue'
import UpdatePass from './views/updatePass.vue'
import Author from './views/author.vue'
import Talking from './views/talking.vue'
import Theme from './views/theme.vue'

export const Bus = new Vue()


Vue.config.productionTip = false
Vue.prototype.$axios = axios;
//安装插件，挂载属性
Vue.use(scroll);
Vue.use(VueRouter);
Vue.use(ElementUI);


let router = new VueRouter({
  mode: 'history', //解决访问路径里的#问题
  //路由配置
  routes:[
    {
      path: '/',
      component:Index
    },
    {
      path:'/story',
      component:StoryList,
      name:'storyList'
    },
    {
      path:'/storyDetail',
      component:StoryDetail,
      name:'storyDetail'
    },
    {
      path:'/writeStory',
      component:WriteStory,
      name:'writeStory'
    },
    // {
    //   path:'/hidden',
    //   component:Hidden,
    // },

    {
      path:'/center',
      component:Center,
      name:'center',
      children:[
        {
          path:'/myStory',
          component:MyStory,
          name:'myStory',
        },
        {
          path:'/comment',
          component:Comment,
          name:'comment',
        },
        {
          path:'/likely',
          component:Likely,
          name:'likely',
        },
        {
          path:'/collect',
          component:Collect,
          name:'collect',
        }

      ]
    },

    {
      path:'/login',
      component:LoginRegister,
      name:'login-register',
    },
    {
      path:'/updateUser',
      component:UpdateUser,
      name:'updateUser',
    },
    {
      path:'/updatePass',
      component:UpdatePass,
      name:'updatePass',
    },
    {
      path:'/author',
      component:Author,
      name:'author',
    },
    {
      path:'/talking',
      component:Talking,
      name:'talking',
    },
    {
      path:'/theme',
      component:Theme,
      name:'theme',
    }
    
  ]
})

import moment from 'moment'
Vue.filter('dateFormat', function (dateStr,pattern = "YYYY-MM-DD HH:mm:ss") {
  return moment(dateStr).format(pattern);
})
Vue.filter('date', function (value, fmt) {
  var dateTime = new Date(value);
  var o = {
      "M+": dateTime.getMonth() + 1,               //月份   
      "d+": dateTime.getDate(),                    //日   
      "h+": dateTime.getHours(),                   //小时   
      "m+": dateTime.getMinutes(),                 //分   
      "s+": dateTime.getSeconds(),                 //秒   

  };

  if (/(y+)/.test(fmt)) {
      fmt = fmt.replace(RegExp.$1, (dateTime.getFullYear() + "").substr(4 - RegExp.$1.length));
  }
  for (var k in o) {
      if (new RegExp(`(${k})`).test(fmt)) {
          fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
      }
  }
  return fmt;
})


//路由拦截
router.beforeEach((to, from, next)=>{
	var login_key = 'false'
	login_key= window.localStorage.getItem('flag')
  console.log(login_key)
	if(to.path === '/center' && login_key=='false'){
		next('/login');
	}else {
		next();
	}
})

new Vue({
  router,
  render: h => h(App),
}).$mount('#app')
